package com.fivec.javaproject.service;

import com.alibaba.fastjson.JSONObject;
import com.fivec.javaproject.Constants;
import com.fivec.javaproject.bean.Weather;
import com.fivec.javaproject.bean.WeatherCommon;
import com.fivec.javaproject.net.HttpUtils;

/**
 * @author wangyizhu
 * @version "1.8.0_131"
 * @email 2641956765@qq.com
 * @desc:
 * @Time 2021/12/10 14:28
 */
public class WeatherService {
    /**
     * 该方法用于获取天气情况，并将数据返回
     *
     * @param city
     * @return Weather
     */
    public Weather getWeatherMessage(String city) {
        String url = Constants.WEATHER_URL
                + "?city=" + city +
                "&key=" + Constants.WEATHER_KEY;
        String result = HttpUtils.get(url);
        //System.out.println(result);
        //json解析
        WeatherCommon weather = JSONObject.parseObject(result, WeatherCommon.class);
        return weather.result;
    }
}
